#ifndef STX_ALGORITHM_PARTIAL_SUM_HPP
#define STX_ALGORITHM_PARTIAL_SUM_HPP

#include <stx/algorithm/common.hpp>

namespace stx {

template <class InputIterator, class OutputIterator>
inline OutputIterator
partial_sum(InputIterator first, InputIterator last, OutputIterator result)
{
    if (first != last) {
        typename std::iterator_traits<InputIterator>::value_type t(*first);
        *result = t;
        for (++first, ++result; first != last; ++first, ++result) {
            t = t + *first;
            *result = t;
        }
    }
    return result;
}

template <class InputIterator, class OutputIterator, class BinaryOperation>
inline OutputIterator
partial_sum(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    BinaryOperation binary_op)
{
    if (first != last) {
        typename std::iterator_traits<InputIterator>::value_type t(*first);
        *result = t;
        for (++first, ++result; first != last; ++first, ++result) {
            t = binary_op(t, *first);
            *result = t;
        }
    }
    return result;
}

} // namespace stx

#endif // STX_ALGORITHM_PARTIAL_SUM_HPP
